var Bullet = function(x,y){
    this.x=x ;
    this.y=y;
     
    this.drawBullet = function (){
        spaceContext.beginPath();
        spaceContext.moveTo(this.x, this.y);
        spaceContext.lineTo(this.x, this.y-10);
        spaceContext.lineWidth=5;
        spaceContext.strokeStyle = 'white';
        spaceContext.stroke();

    }
    
    this.detectTargetEnemy=function(){
        var done=false;
        var enemyCol=Math.round(((this.x-grid.leftMargin)/grid.hSpace));
        var gridLowerEdge=grid.topMargin+(grid.images.length*grid.vSpace);
        if(enemyCol>=0 && enemyCol<grid.rowImages){
            for(var index=grid.images.length-1;index>=0 && !done && this.y< gridLowerEdge ;index--){
                if(!grid.gridElements[index][enemyCol].exploded){                    
                    grid.gridElements[index][enemyCol].exploded=true;
                    grid.gridElements[index][enemyCol].source='explosion.png';
                    grid.gridElements[index][enemyCol].updateScore();
                    drawScore();
                    done=true;
                }
            }
        }
        return done;
    }
    
    this.update=function(){
        var result;
        if(!this.detectTargetEnemy()){
            spaceContext.clearRect(0,0,spaceCanvas.width,spaceCanvas.height);
            this.y-=20;
            this.drawBullet();
            if(this.y<=0){
                result=false; 
            }
            result=true;
        }else{
            grid.killedEnemiesCounter++;
            result=false;
        }
        
        return result;
    }
}
